package com.gxa.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.admin.entity.Shop;
import com.gxa.admin.mapper.ShopMapper;
import com.gxa.admin.param.ShopParam;
import com.gxa.admin.service.ShopService;
import com.gxa.o2o.core.dto.ResultDTO;
import com.gxa.o2o.core.utils.Response;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Shop)表服务实现类
 *
 * @author makejava
 * @since 2021-07-20 20:57:31
 */
@Service("shopService")
public class ShopServiceImpl implements ShopService {
    @Resource
    private ShopMapper shopMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param shopId 主键
     * @return 实例对象
     */
    @Override
    public ResultDTO queryById(Long shopId) {
        Shop shop = this.shopMapper.queryById(shopId);
        return Response.success(200,"success",shop);

    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Shop> queryAllByLimit(int offset, int limit) {
        return this.shopMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param shop 实例对象
     * @return 实例对象
     */
    @Override
    public Shop insert(Shop shop) {
        this.shopMapper.insert(shop);
        return shop;
    }

    /**
     * 修改数据
     *
     * @param shop 实例对象
     * @return 实例对象
     */
    @Override
    public ResultDTO update(Shop shop) {
        int i = this.shopMapper.update(shop);
        if (i>0){
            return Response.success("修改成功");
        }
        return Response.error(1001, "修改失败");
    }

    /**
     * 通过主键删除数据
     *
     * @param shopId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long shopId) {
        return this.shopMapper.deleteById(shopId) > 0;
    }

    @Override
    public ResultDTO findByAll(ShopParam shopParam) {
        System.out.println(shopParam);
        PageHelper.startPage(shopParam.getPage(),shopParam.getLimit());
        List<Shop> shopList = shopMapper.findByAll(shopParam);

        PageInfo<Shop> shopPageInfo = new PageInfo<>(shopList);

        return Response.success(200, "success", shopPageInfo);
    }
}
